package com.hh.spare.controller;/*
 * @Auther:ZhengXueCheng
 * @Date:2023/7/28
 * @VERSON:1.8
 */

import com.hh.spare.service.SpareService;
import com.hh.spare.vo.AddSpareVo;
import com.hh.spare.vo.SpareVo;
import com.hh.spare.vo.StatusChangeVo;
import com.hh.spare.vo.UpdateSpareVo;
import com.hh.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/spare")
@CrossOrigin
public class SpareController {
    @Autowired
    private SpareService spareService;

    @PostMapping("/queryPage")
    public ResultVo queryPage(@RequestBody SpareVo spareVo) {
        return ResultVo.success(spareService.findAllByPage(spareVo));
    }

    @PostMapping("/updateStatus")
    public ResultVo updateStatus(@RequestBody StatusChangeVo statusChangeVo) {
        return spareService.updateStatus(statusChangeVo);
    }

    @GetMapping("/queryById")
    public ResultVo queryById(@RequestParam("id") Integer id) {
        return spareService.GetLegalPersonId(id);
    }

    @PostMapping("/addSpare")
    public ResultVo addSpare(@RequestBody @Valid AddSpareVo addSpareVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return spareService.addSpare(addSpareVo);
    }

    @PostMapping("/updateSpare")
    public ResultVo updateSpare(@RequestBody @Valid UpdateSpareVo updateSpareVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return spareService.updateSpare(updateSpareVo);
    }
}
